package global;

import java.lang.Math.*;

/**
 *
 * @author Jack
 */
public class Vector100Dtype {
    
    private short[] vector100D = new short[100];
    
    /**
     *  constructor of class
     */
    public Vector100Dtype()
    {
        for(int n = 0; n < 100; n++)
            vector100D[n] = 0;
    }
    
    Vector100Dtype(String input)
    {
         String[] stringArray = input.split("\\s+");
         fillVector(stringArray);
    }
    Vector100Dtype(String[] input)
    {
    	if (input.length >= 100)
    	{
    		fillVector(input);
    	}
    }
    private void fillVector(String[] stringArray)
    {
        
        for(int n = 0; n < 100; n++)
        {
            if(stringArray[n] != null)
            {
                short temp = Short.parseShort(stringArray[n]);
                
                if(temp < -10000)
                    vector100D[n] = -10000;
                else if(temp > 10000)
                    vector100D[n] = 10000;
                else
                    vector100D[n] = temp;
            }
            else
            {
                // ???
            }
        }
    }
    
    public short getIndex(int n)
    {
        return vector100D[n];
    }
    
    public void setIndex(int n, short val)
    {
        vector100D[n] = val;
    }
    
    /** @author Mark Yates
     * Converts the vector to a string representation for the form:
     * <1,2,1,2,4,5,6,...>
     */
    public String toString()
    {
    	String vector = "<";
    	for (int i=0; i<99; i++)
    	{
    		vector = vector + String.format("%d,", vector100D[i]);
    	}
    	vector = vector + String.format("%d>", vector100D[99]);
    	return vector;
    }
    
    // @author: Mark Yates
    // Calculates the distance between two vectors (uses Euclidean distance)
    public static double Distance(Vector100Dtype v1, Vector100Dtype v2)
    {
    	long sumOfSquares = DistanceForSort(v1, v2);
    	return Math.sqrt(sumOfSquares);
    }
    
    // @author: Mark Yates
    // Calculates a relative distance between vectors that can be used for sorting/
    // This basically the Euclidean distance squared. By not performing the square root
    // of the sum of squares, we can save calculation time.
    public static long DistanceForSort(Vector100Dtype v1, Vector100Dtype v2)
    {
    	long sumOfSquares = 0;
    	for (int i=0; i<100; i++)
    	{
    		sumOfSquares += (v1.vector100D[i] - v2.vector100D[i]) * (v1.vector100D[i] - v2.vector100D[i]);
    	}
    	return sumOfSquares;
    }
    
    private int EDistance(Vector100Dtype v2)
    {
        // calculate and return the euclidean distance
        int temp = 0;
        
        for(int n = 0; n < 100; n++)
        {
            temp += (vector100D[n] - v2.getIndex(n)) * (vector100D[n] - v2.getIndex(n));
        }
        
        return (int)Math.sqrt((double)temp);
    }
}
